#include "Common.h"

string id2Str(int id)
{
	char c[5];
	int i,m,t=id;
	for (i=0;i<4;i++)
	{
		m=t%256;
		c[i]=m;
		if (!m) break;
		t>>=8;
	}
	c[4]=0;
	return string(c);
}
int str2Id(string str)
{
	int l,id=0;
	l=str.length();
	if (l!=4) return 0;
	for (l=0;l<4;l++)
	{
		id<<=8;
		id+=str[3-l];
	}
	return id;
}

void readIFS(ifstream &ifs,int &var)
{
	ifs.read((char*)(&var),4);
}
void readIFS(ifstream &ifs,float &var)
{
	ifs.read((char*)(&var),4);
}
void readIFS(ifstream &ifs,string &var)
{
	char temp;
	var="";
	while (1)
	{
		ifs.read(&temp,1);
		if (temp) var+=temp;
		else break;
	}
}

void writeOFS(ofstream &ofs,int &var)
{
	ofs.write((char*)(&var),4);
}
void writeOFS(ofstream &ofs,float &var)
{
	ofs.write((char*)(&var),4);
}
void writeOFS(ofstream &ofs,string &var)
{
	int i,len=var.length();
	char temp;
	for (i=0;i<len;i++)
	{
		temp=var[i];
		ofs.write(&temp,1);
	}
	temp=0;
	ofs.write(&temp,1);
}
